﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESHOP.DAO;
using ESHOP.UTIL;
using ESHOP.Domain;
using E_Shop.Common;
using Microsoft.Reporting.WebForms;

namespace E_Shop
{
    public partial class CompanyMail :Authentication
    {
        private HopthuDAO _mailDao = new HopthuDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Acount != null)
            {
                LoadDatanew();
                LoaddataOld();
                Loaddataoutbox();
                LoadDelete();
            }
            else
            {
                Response.Redirect(WebUrl.LOGIN);
            }
        }

        private void LoadDelete()
        {
            var listdelete = _mailDao.Getlistdeletecompanies(Acount.MACT);
            Gvdelete.DataSource = listdelete;
            Gvdelete.DataBind();
            upGirddelete.Update();
        }
        private void Loaddataoutbox()
        {
            var lisoutbox = _mailDao.Getlistoutboxbycompanies(Acount.MACT);
            gvoutbox.DataSource = lisoutbox;
            gvoutbox.DataBind();
            Upgirdoutbox.Update();
        }
        private void LoadDatanew()
        {
            
            var mailList = _mailDao.GetListbyCompaniesReceiver_new(Acount.MACT);
            gvListEmail.DataSource = mailList;
            gvListEmail.DataBind();
            UpGrid.Update();

        }
        public void LoaddataOld()
        {
            var mailList = _mailDao.GetListbyCompaniesReceiver_old(Acount.MACT);
            Gvlist_inbox_read.DataSource = mailList;
            Gvlist_inbox_read.DataBind();
            Upgridreadinbox.Update();
        }
        private void UnblockDialog(string divId)
        {
            ((Company)Page.Master).UnblockDialog(divId);
        }
        private void HideDialog(string divId)
        {
            ((Company)Page.Master).HideDialog(divId);
        }
        protected void gvListSP_RowCommand(object seder, GridViewCommandEventArgs e)
        {
            var idmail = e.CommandArgument.ToString();
            var objmail=_mailDao.Get(idmail);
            var idname = e.CommandName;
            switch (idname)
            {
                case "deleteemail":
                    {
                        if(objmail!=null)
                        {

                            if (_mailDao.DeleteMail(objmail.ID) == 1)
                            {
                                ShowError("Delete email success.");
                            }
                            else
                            {
                                ShowError("Delete email fail.");
                            }
                            LoadDatanew();
                            LoadDelete();
                            UpGrid.Update();
                           
                        }
                        break;
                    }
                case "detailsmail":
                    {
                        Session[Constants.SESSION_EMAIL] = objmail;
                        objmail.IsRead = true;
                        _mailDao.Updateread(objmail);
                        UnblockDialog("detailsmail");
                        Bindmail(objmail);
                        LoadDatanew();
                        UpGrid.Update();
                        updetailsmail.Update();
                        break;
                    }

            }
        }

        protected void gvListEmail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (!e.Row.RowType.Equals(DataControlRowType.DataRow)) return;
            var lbtenquocgia = e.Row.FindControl("lbTenQuocGia") as Label;
            if (lbtenquocgia == null)
                return;
            var list = gvListEmail.DataSource as List<HOPTHU>;
            var hopthu = list[e.Row.RowIndex];            
            lbtenquocgia.Text = hopthu.CONGTY.QUOCGIA.TENQUOCGIA;
        }

        protected void gvListEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListEmail.PageIndex = e.NewPageIndex;
            gvListEmail.DataBind();
        
        }

        protected void Gvlist_inbox_read_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
            var idmail = e.CommandArgument.ToString();
            var objmail=_mailDao.Get(idmail);
            var idname = e.CommandName;
            switch (idname)
            {
                case "deleteemail":
                    {
                        if(objmail!=null)
                        {
                            if (_mailDao.DeleteMail(objmail.ID) == 1)
                            {
                                ShowInfor("Delete email success.");
                            }
                            else
                            {
                                ShowError("Delete email fail!.");
                            }
                            LoaddataOld();
                            LoadDelete();
                            Upgridreadinbox.Update();
                        }
                        break;
                    }
                case "detailsmail":
                    {
                        Session[Constants.SESSION_EMAIL] = objmail;
                        UnblockDialog("detailsmail");
                        Bindmail(objmail);
                        LoadDatanew();
                        UpGrid.Update();
                        updetailsmail.Update();
                        break;
                    }
                case "replyemail":
                    {
                        Session[Constants.SESSION_EMAIL] = objmail;
                        UnblockDialog("replyemail");
                        Bindreplymail(objmail);
                        LoadDatanew();
                        LoadDelete();
                        Upreplyemail.Update();
                        break;
                    }

            }
        }

        private void Bindreplymail(HOPTHU objmail)
        {
            lbreceiver.Text = objmail.CONGTY1.TENCONGTY;
            lbngaygui.Text = objmail.NGAYGUI.ToString();
           

        }

        private void Bindmail(HOPTHU objmail)
        {

            rpViewer.LocalReport.DataSources.Clear();
            rpViewer.LocalReport.ReportPath = "emaildetails.rdlc";
            ReportParameter rp = new ReportParameter("Ngaygui", objmail.NGAYGUI.ToString());
            ReportParameter rp2 = new ReportParameter("nguoigui", objmail.CONGTY1.TENCONGTY.ToString());
            ReportParameter rp3 = new ReportParameter("tieude", objmail.SUBJECT.ToString());
            ReportParameter rp4 = new ReportParameter("noidung", objmail.CONTENT_MSG.ToString());
            rpViewer.LocalReport.SetParameters(new ReportParameter[] { rp, rp2, rp3,rp4 });
            rpViewer.LocalReport.Refresh();
            updetailsmail.Update();
        }

      
        
        protected void Gvlist_inbox_read_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }
        protected void Gvlist_inbox_read_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            Gvlist_inbox_read.PageIndex = e.NewPageIndex;
            Gvlist_inbox_read.DataBind();
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            UnblockDialog("detailsmail");
            
        }

       
        protected void btnsend_Click(object sender, EventArgs e)
        {
            HideDialog("replyemail");
            var objmail = (HOPTHU)Session[Constants.SESSION_EMAIL];
            HOPTHU _emmail = new HOPTHU();
            _emmail.ID = _mailDao.NewId();
            _emmail.SUBJECT = String.IsNullOrEmpty(txtSubject.Text.Trim()) ? "" : txtSubject.Text.Trim();
            _emmail.CONTENT_MSG = txtcontent.Text.Trim() == null ? "" : txtcontent.Text.Trim();
            _emmail.SENDER = Acount.MACT;
            _emmail.RECEIVER = objmail.SENDER;
            _emmail.IsRead = false;
            _emmail.ISSHOW = true;
            _emmail.NGAYGUI = DateTime.Now;
            int kq=_mailDao.Insert(_emmail);
            if (kq == 1)
            {
                ShowInfor("Send email success.");
            }

            else
            {
                ShowError("Send email fail!.");
            }

            
        }

        protected void gvoutbox_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var idmail = e.CommandArgument.ToString();
            var objmail = _mailDao.Get(idmail);
            var idname = e.CommandName;
            switch (idname)
            {
                case "deleteemail":
                    {
                        if (objmail != null)
                        {
                            _mailDao.DeleteMail(objmail.ID);
                            LoaddataOld();
                            Upgirdoutbox.Update();
                        }
                        break;
                    }
                case "detailsmail":
                    {
                        Session[Constants.SESSION_EMAIL] = objmail;
                        UnblockDialog("detailsmail");
                        Bindmail(objmail);
                        break;
                    }
            }
        }

        protected void gvoutbox_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvoutbox_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvoutbox.PageIndex = e.NewPageIndex;
            gvoutbox.DataBind();
        }

        protected void Gvdelete_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var idmail = e.CommandArgument.ToString();
            var objmail = _mailDao.Get(idmail);
            var idname = e.CommandName;
            switch (idname)
            {
               
                case "detailsmail":
                    {
                        Session[Constants.SESSION_EMAIL] = objmail;
                        UnblockDialog("detailsmail");
                        Bindmail(objmail);
                        break;
                    }
            }
        }

        protected void Gvdelete_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            Gvdelete.PageIndex = e.NewPageIndex;
            Gvdelete.DataBind();
        }

        protected void Gvdelete_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }
    }
}